package com.mingteng.mobile_feedback.respository.impl;

import com.mingteng.mobile_feedback.domin.Mapper.PlanInfoMapper;
import com.mingteng.mobile_feedback.domin.Mapper.ViewMesScInfoMapper;
import com.mingteng.mobile_feedback.domin.PlanInfo;
import com.mingteng.mobile_feedback.domin.ViewMesScInfoList;
import com.mingteng.mobile_feedback.respository.JdbcRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class JdbcRepositoryImpl implements JdbcRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<ViewMesScInfoList> getViewScInfoList(String orderId,String gxName) {
        String sql = "SELECT top 6 Gx_Code,Gx_Name,JH_Zx_Bz FROM [MES].[dbo].[View_Mes_ScInfo_List] WHERE  ScOrder_Code='" + orderId + "' AND  Forder < " +
                "  (SELECT  Forder from [MES].[dbo].[View_Mes_ScInfo_List] where ScOrder_Code='" + orderId + "' and  Gx_Code = '" + gxName + "' ) ORDER BY Forder desc";

        List<ViewMesScInfoList> list = jdbcTemplate.query(sql, new ViewMesScInfoMapper());
        return list;
    }

    @Override
    public List<PlanInfo> getPlanList(String userCode) {
        String sql = "select ISNULL(JH,ScOrder_Code)AS JH,a.ScOrder_Code,a.ProductName,Gx_Name,JHKSDate,JhDate,JH_Zx_Bz,cast(a.Pweight as varchar ) +'吨' as Pweight ,a.CustomerName,a.Gx_Code,b.OrderNeedDate  FROM [MES].[dbo].[View_Mes_ScInfo_List] a LEFT JOIN [MES].[dbo].[Tab_MES_ScInfo_XsOrder] b on a.Order_Code = b.Order_Code  where  GETDATE() between DATEADD(day,-7,JHKSDate) and DATEADD(day,7,JhDate) ";
        if(userCode.equals("system")){
            sql+= "and Gx_Code in (select gx_code  FROM [MES].[dbo].[Tab_MES_BaseManage_Gx_User] where UserCode ='"+ userCode+ "')";
        }

        List<PlanInfo> list = jdbcTemplate.query(sql, new PlanInfoMapper());
        return list;
    }

    @Override
    public boolean updateScOrder(String id) {
        String sql = "update [MES].[dbo].[Tab_MES_ScInfo_ScOrder] set Amount = 0,Remark='报废' where ScOrder_Code in (select ScOrder_Code from [MES].[dbo].[Tab_MES_QualityManage_EvaluteResults] where Id = '" + id + "')";
        int ret = jdbcTemplate.update(sql);
        return ret> 0 ? true:false;
    }

    @Override
    public boolean insertJudgeInfo(String id) {
        String sql = "insert into  [MES].[dbo].[Tab_MES_QualityManage_JudgeInfo] " +
                " select newId(),a.Id,b.Sp_Jb,b.Sp_Sx,'','',b.UserId,b.UserName " +
                "  from [MES].[dbo].[Tab_MES_QualityManage_EvaluteResults] a inner join (select * from   [MES].[dbo].[Tab_MES_BaseManage_SP_Detail] where Sp_Name = '质量审批' ) as b " +
                "  on  b.Sp_Jb = '2'   and a.Id =" + id + "  order by a.Id,Sp_Jb,Sp_Sx asc";
        int ret = jdbcTemplate.update(sql);
        return ret> 0 ? true:false;
    }
}
